<?php

namespace app\system\controller;

use AlibabaCloud\Credentials\Credential\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Exception;

/**
 * 验证码操作处理
 *
 * @author xuxiwang
 */
class Captcha extends Base
{

    public function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            // 您的 AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的 AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }


    /**
     *  //todo 短信验证码
     */
    public function smsCaptcha()
    {
        $client = self::createClient("accessKeyId", "accessKeySecret");
        $sendSmsRequest = new SendSmsRequest([]);
        $runtime = new RuntimeOptions([]);
        try {
            // 复制代码运行请自行打印 API 的返回值
            $client->sendSmsWithOptions($sendSmsRequest, $runtime);
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // 如有需要，请打印 error
            Utils::assertAsString($error->message);
        }
    }

    /**
     * 生成验证码
     */
    public function captchaImage(): \think\response\Json
    {
        $captcha = captcha();

        $image_data_base64 = base64_encode($captcha->getData());

        $returnJson = [
            'captchaEnabled'=> true,
            'img'=> $image_data_base64,
            'msg'=> '操作成功',
            'uuid'=> 'd2f6ca319e0c4f2283fb3b1557a6a24e',
            'code'=> 200
        ];
        return json($returnJson);
    }
}